<?php
/**
 * Created by PhpStorm.
 * User: fuguang
 * Date: 2017/10/16
 * Time: 下午4:44
 */

namespace frontend\controllers;
use Yii;
use yii\helpers\Html;
use common\controllers\FrontendController;
use yii\filters\AccessControl;
use frontend\wx\WXBizMsgCrypt;

class WxController extends FrontendController
{
    public $layout = "main-move";

    public $encodingAesKey = "A2xQYsZer4M2pEjkhu37VIdPeAYQwjV1jp4zFIAQMDq";
    public $token = "carmv123";
    public $nonce = "xxxxxx";
    public $appId = "wxd1f20a49599ddac6";

    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index','push','resave'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
        ];
    }

    public function actionIndex(){
        $notice_userid = Yii::$app->request->get("key",0); //需要通知的用户

    }

    // 第三方发送消息给公众平台
    public function actionPush(){
        $signature = Yii::$app->request->get("signature");
        $timestamp = Yii::$app->request->get("timestamp");
        $nonce = Yii::$app->request->get("nonce");
        $echostr = Yii::$app->request->get("echostr");

        echo $echostr;exit;
        $msg = '';
        $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
        $from_xml = sprintf($format, $echostr);
        $pc = new WXBizMsgCrypt($this->token, $this->encodingAesKey, $this->appId);
        $errCode = $pc->decryptMsg($signature, $timestamp, $nonce, $from_xml, $msg);
        if ($errCode == 0) {
            print("解密后: " . $msg . "\n");
        } else {
            print($errCode . "\n");
        }

exit;

        $text = "<xml><ToUserName><![CDATA[oia2Tj我是中文jewbmiOUlr6X-1crbLOvLw]]></ToUserName><FromUserName><![CDATA[gh_7f083739789a]]></FromUserName><CreateTime>1407743423</CreateTime><MsgType><![CDATA[video]]></MsgType><Video><MediaId><![CDATA[eYJ1MbwPRJtOvIEabaxHs7TX2D-HV71s79GUxqdUkjm6Gs2Ed1KF3ulAOA9H1xG0]]></MediaId><Title><![CDATA[testCallBackReplyVideo]]></Title><Description><![CDATA[testCallBackReplyVideo]]></Description></Video></xml>";
        $pc = new WXBizMsgCrypt($this->token, $this->encodingAesKey, $this->appId);
        $encryptMsg = '';
        $errCode = $pc->encryptMsg($text, $timestamp, $nonce, $encryptMsg);
        if ($errCode == 0) {
            print("加密后: " . $encryptMsg . "\n");
        } else {
            print($errCode . "\n");
        }

        $xml_tree = new \DOMDocument();
        $xml_tree->loadXML($encryptMsg);
        $array_e = $xml_tree->getElementsByTagName('Encrypt');
        $array_s = $xml_tree->getElementsByTagName('MsgSignature');
        $encrypt = $array_e->item(0)->nodeValue;
        $msg_sign = $array_s->item(0)->nodeValue;

        $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
        $from_xml = sprintf($format, $encrypt);


    }

    // 第三方收到公众号平台发送的消息
    public function actionResave(){
        $msg = '';
        $pc = new WXBizMsgCrypt($this->token, $this->encodingAesKey, $this->appId);
        $errCode = $pc->decryptMsg($this->msg_sign, $this->timeStamp, $this->nonce, $this->from_xml, $msg);
        if ($errCode == 0) {
            print("解密后: " . $msg . "\n");
        } else {
            print($errCode . "\n");
        }
    }
}